package com.junxonline.instant.common.entity.common;

import cn.hutool.http.HttpStatus;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.junxonline.instant.common.enumeration.AdminErrorEnum;
import com.junxonline.instant.common.enumeration.ApiErrorEnum;
import lombok.Data;

import java.io.Serializable;

/**
 * 公共响应对象
 *
 * @author JunX
 * @date 2021-06-15
 */
@Data
public class CommonResult implements Serializable {

    /**
     * 响应码
     */
    private Integer code;

    /**
     * 响应信息
     */
    private String msg;

    /**
     * 响应数据
     */
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private Object data;

    private static final String OK = "成功";

    private static final String INTERNAL_SERVER_ERROR = "服务器发生错误";

    /**
     * 响应成功
     *
     * @return CommonResult
     */
    public static CommonResult success() {
        CommonResult commonResult = new CommonResult();
        commonResult.setCode(HttpStatus.HTTP_OK);
        commonResult.setMsg(OK);
        return commonResult;
    }

    /**
     * 响应成功
     *
     * @return CommonResult
     */
    public static CommonResult success(String msg) {
        CommonResult commonResult = new CommonResult();
        commonResult.setCode(HttpStatus.HTTP_OK);
        commonResult.setMsg(msg);
        return commonResult;
    }

    /**
     * 响应成功
     *
     * @return CommonResult
     */
    public static CommonResult success(Object data) {
        CommonResult commonResult = new CommonResult();
        commonResult.setCode(HttpStatus.HTTP_OK);
        commonResult.setMsg(OK);
        commonResult.setData(data);
        return commonResult;
    }

    /**
     * 响应失败
     *
     * @return CommonResult
     */
    public static CommonResult fail() {
        CommonResult commonResult = new CommonResult();
        commonResult.setCode(HttpStatus.HTTP_INTERNAL_ERROR);
        commonResult.setMsg(INTERNAL_SERVER_ERROR);
        return commonResult;
    }

    /**
     * 响应失败
     *
     * @return CommonResult
     */
    public static CommonResult fail(Integer code, String msg) {
        CommonResult commonResult = new CommonResult();
        commonResult.setCode(code);
        commonResult.setMsg(msg);
        return commonResult;
    }

    /**
     * 响应失败
     *
     * @return CommonResult
     */
    public static CommonResult fail(AdminErrorEnum adminErrorEnum) {
        CommonResult commonResult = new CommonResult();
        commonResult.setCode(adminErrorEnum.getCode());
        commonResult.setMsg(adminErrorEnum.getMsg());
        return commonResult;
    }

    /**
     * 响应失败
     *
     * @return CommonResult
     */
    public static CommonResult fail(ApiErrorEnum apiErrorEnum) {
        CommonResult commonResult = new CommonResult();
        commonResult.setCode(apiErrorEnum.getCode());
        commonResult.setMsg(apiErrorEnum.getMsg());
        return commonResult;
    }

}
